package org.example.stockdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.stockdemo.dto.StockMonitorDTO;
import org.example.stockdemo.entity.StockDetail;
import org.example.stockdemo.entity.StockMonitor;
import org.example.stockdemo.mapper.StockMonitorMapper;
import org.example.stockdemo.service.StockDetailService;
import org.example.stockdemo.service.StockGroupItemService;
import org.example.stockdemo.service.StockMonitorService;
import org.example.stockdemo.service.TencentStockService;
import org.example.stockdemo.util.BeanExtUtil;
import org.example.stockdemo.util.CacheUtil;
import org.example.stockdemo.vo.StockMonitorVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class StockMonitorServiceImpl extends ServiceImpl<StockMonitorMapper, StockMonitor> implements StockMonitorService {

    @Autowired
    private StockMonitorMapper stockMonitorMapper;
    @Autowired
    private TencentStockService tencentStockService;
    @Autowired
    private StockDetailService stockDetailService;
    @Autowired
    private StockGroupItemService groupItemService;

    @Override
    public boolean batchSave(List<StockMonitor> stockMonitorList) {
        return false;
    }

    @Override
    public void addMonitor(StockMonitorDTO stockMonitorDTO) {

        // 添加监视标志
        groupItemService.addMonitorFlag(stockMonitorDTO.getGroupId(), stockMonitorDTO.getCode());

        // 查询股票监视数据
        StockMonitor todayStockMonitor = stockMonitorMapper.selectOne(new LambdaQueryWrapper<StockMonitor>().eq(StockMonitor::getCode, stockMonitorDTO.getCode()));
        if (todayStockMonitor != null) {
            // 更新价格
            todayStockMonitor.setCostPrice(stockMonitorDTO.getCostPrice());
            todayStockMonitor.setMonitorPrice(stockMonitorDTO.getMonitorPrice());
            stockMonitorMapper.updateById(todayStockMonitor);
            return;
        }

        fillPrice(stockMonitorDTO);

    }

    /**
     * 查询详情，填充5日、10日价格，昨收价，开盘价格，开盘涨幅
     *
     * @param stockMonitorDTO
     * @return
     */
    @Transactional
    @Override
    public StockMonitor fillPrice(StockMonitorDTO stockMonitorDTO) {
        // 查询详情
        StockDetail stockDetail = stockDetailService.getStockDetailByCode(stockMonitorDTO.getCode());

        // 获取10日、5日价格
        List<StockDetail> klineData = tencentStockService.getKlineData(stockDetail.getCode(), 10);
        StockMonitor stockMonitor = new StockMonitor();
        BeanUtils.copyProperties(stockDetail, stockMonitor);
        double fiveDayPrice = 0;
        double tenDayPrice = 0;
        // 遍历klineData，计算5日均线价格、10日均线价格
        for (int i = 0; i < klineData.size(); i++) {
            tenDayPrice += klineData.get(i).getPrice();
            if (i > 4) {
                fiveDayPrice += klineData.get(i).getPrice();
            }
        }
        stockMonitor.setCostPrice(stockMonitorDTO.getCostPrice());
        stockMonitor.setMonitorPrice(stockMonitorDTO.getMonitorPrice());
        stockMonitor.setFiveDayPrice(fiveDayPrice / 5);
        stockMonitor.setTenDayPrice(tenDayPrice / 10);
        // 计算开盘涨幅
        Double yesterdayPrice = stockMonitor.getYesterdayPrice();
        double startPencent = (stockMonitor.getStartPrice() - yesterdayPrice) / yesterdayPrice * 100;
        stockMonitor.setStartChangePercent(startPencent);
        // 保存
        save(stockMonitor);
        return stockMonitor;
    }

    @Override
    public StockMonitorVO detail(String stockCode) {
        StockMonitor todayStockMonitor = stockMonitorMapper.selectOne(new LambdaQueryWrapper<StockMonitor>().
                eq(StockMonitor::getCode, stockCode));
        if (todayStockMonitor != null) {
            return BeanExtUtil.copyTo(todayStockMonitor, StockMonitorVO.class);
        }
        return new StockMonitorVO();
    }

}